package fr.afcepf.iziticket.beans;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import fr.afcepf.iziticket.dao.interfaces.DAOAudience;
import fr.afcepf.iziticket.dao.interfaces.DAOGenre;
import fr.afcepf.iziticket.dao.interfaces.DAOSpectacle;
import fr.afcepf.iziticket.entities.Audience;
import fr.afcepf.iziticket.entities.Genre;
import fr.afcepf.iziticket.entities.Spectacle;

@ManagedBean(name = "mbRecherche")
@SessionScoped
public class RechercheManagedBean {

	private String nom;
	private String lieu;
	private String dateDeb;
	private String dateFin;
	private int prixKey;
	private String audience;
	private int idGenreSelected;
	private List<SelectItem> listeGenres = new ArrayList<SelectItem>();
	private int idAudienceSelected;
	private List<SelectItem> listeAudiences = new ArrayList<SelectItem>();
	private int nbJourDateAdd = -1;
	private List<SelectItem> listeDates = new ArrayList<SelectItem>();
	private List<Spectacle> lesSpectacles = new ArrayList<Spectacle>();

	public void init() {

		DAOGenre daoG = (DAOGenre) MainManagedBean.getContext().getBean("daoGenre");
		List<Genre> lesGenres = new ArrayList<Genre>();
		lesGenres = daoG.getAll();
		listeGenres.clear();
		SelectItem siG = new SelectItem(0, "-");
		listeGenres.add(siG);
		for (Genre genre : lesGenres) {
			siG = new SelectItem(genre.getIdGenre(), genre.getNomGenre());
			listeGenres.add(siG);
		}

		DAOAudience daoA = (DAOAudience) MainManagedBean.getContext().getBean("daoAudience");
		List<Audience> lesAudiences = new ArrayList<Audience>();

		lesAudiences = daoA.getAll();

		listeAudiences.clear();
		SelectItem siA = new SelectItem(0, "-");
		listeAudiences.add(siA);
		for (Audience audience : lesAudiences) {
			siA = new SelectItem(audience.getIdaudience(),
					audience.getNomAudience());
			listeAudiences.add(siA);
		}
		
		listeDates.clear();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
		Date dateNow = new Date();
		@SuppressWarnings("unused")
		java.sql.Date dateSQL = new java.sql.Date(dateNow.getTime());
		SelectItem siD = new SelectItem(-1, "-");
		listeDates.add(siD);
		siD = new SelectItem(0, "aujourd'hui");
		listeDates.add(siD);
		
		for (int nbJour = 1; nbJour < 30; nbJour++){
			Calendar cal = Calendar.getInstance();
			cal.setTime(dateNow);
			cal.add(Calendar.DATE, nbJour);
			String dateStr = simpleDateFormat.format(cal.getTime());
			siD = new SelectItem(nbJour, dateStr);
			listeDates.add(siD);
		}
	}

	public RechercheManagedBean() {
		init();
	}

	public int getResultSize(){
		return lesSpectacles.size();
	}
	
	public String rechercher() {

		// System.out.println(genreSelected.getNomGenre());
		// System.out.println(genreSelected.getIdGenre());
		// System.out.println(audienceSelected.getIdaudience());
		// System.out.println(nbJourDateAdd);
		
		int prixMin, prixMax;
		if (prixKey == 0) {
			prixMax = 100000;
			prixMin = 0;
		} else if (prixKey == 1) {
			prixMax = 20;
			prixMin = 0;
		} else if (prixKey == 2) {
			prixMax = 40;
			prixMin = 20;
		} else {
			prixMax = 100000;
			prixMin = 40;
		}

		DAOSpectacle dao = (DAOSpectacle) MainManagedBean.getContext().getBean(
				DAOSpectacle.class);
		lesSpectacles = dao.rechercheSpectacles(nom, lieu, prixMin, prixMax, 
				idGenreSelected, idAudienceSelected,
				nbJourDateAdd);

		return "resultatRecherchePage";
	}	
	
	public int getNbJourDateAdd() {
		return nbJourDateAdd;
	}

	public void setNbJourDateAdd(int nbJourDateAdd) {
		this.nbJourDateAdd = nbJourDateAdd;
	}

	public List<SelectItem> getListeDates() {
		return listeDates;
	}
	
	public void setListeDates(List<SelectItem> listeDates) {
		this.listeDates = listeDates;
	}

	public List<SelectItem> getListeAudiences() {
		return listeAudiences;
	}

	public void setListeAudiences(List<SelectItem> listeAudiences) {
		this.listeAudiences = listeAudiences;
	}

	public List<Spectacle> getLesSpectacles() {
		return lesSpectacles;
	}

	public void setLesSpectacles(List<Spectacle> lesSpectacles) {
		this.lesSpectacles = lesSpectacles;
	}

	public List<SelectItem> getListeGenres() {
		return listeGenres;
	}

	public void setListeGenres(List<SelectItem> listeGenres) {
		this.listeGenres = listeGenres;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getLieu() {
		return lieu;
	}

	public void setLieu(String lieu) {
		this.lieu = lieu;
	}

	public String getDateDeb() {
		return dateDeb;
	}

	public void setDateDeb(String dateDeb) {
		this.dateDeb = dateDeb;
	}

	public String getDateFin() {
		return dateFin;
	}

	public void setDateFin(String dateFin) {
		this.dateFin = dateFin;
	}

	public String getAudience() {
		return audience;
	}

	public void setAudience(String audience) {
		this.audience = audience;
	}

	public int getPrixKey() {
		return prixKey;
	}

	public void setPrixKey(int prixKey) {
		this.prixKey = prixKey;
	}

	public int getIdGenreSelected() {
		return this.idGenreSelected;
	}

	public void setIdGenreSelected(int idGenreSelected) {
		this.idGenreSelected = idGenreSelected;
	}

	public int getIdAudienceSelected() {
		return this.idAudienceSelected;
	}

	public void setIdAudienceSelected(int idAudienceSelected) {
		this.idAudienceSelected = idAudienceSelected;
	}
	
	

}
